package com.kedacom.freedomLabel.view.fragment.setting;

import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;

import com.kedacom.freedomLabel.R;


public class OperatorControlSettingFragment extends PreferenceFragment {

    private SwitchPreference  openPrint, openCom;
//    private static final String MISTAKE_TOUCH_MODE_KEY = "scales_force_record";
    private ListPreference autoExportPre, intervalRecord, beepSound, onlineOfflineRecordData,scalesShowPrice,scalesForceRecordStable,scalesForceRecord
        ,scalesloadAuto,scalesSwitchServer,readCardProduct,showProductPlu;
    private EditTextPreference editTextPreference;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.operator_setting);
        initMistakeTouchPreference();
        bindPreferenceSummaryToValue(findPreference("scales_menu_password"));
//        bindPreferenceSummaryToValue(findPreference("scales_post_url_by_id"));

        bindPreferenceSummaryToValue(findPreference("scales_post_url_post_base"));
        bindPreferenceSummaryToValue(findPreference("scales_post_url_post"));
        bindPreferenceSummaryToValue(findPreference("scales_record_password"));
//        bindPreferenceSummaryToValue(findPreference("interval_recording"));

//        initAutoExport();
//        intervalRecord();
//        initBeepSound();
//        initOnOffRecordDate();
        initShowPrice();
        initForceRecordStable();
        initForceRecord();
        initScalesloadAuto();
//        initScalesSwitchServer();
//        initReadCardProduct();
//        initShowProductPlu();
    }

    private static void bindPreferenceSummaryToValue(Preference preference) {
        preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                PreferenceManager.getDefaultSharedPreferences(preference.getContext()).getString(preference.getKey(), ""));
    }

    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            String stringValue = value.toString();
            if (preference instanceof EditTextPreference) {
                preference.setSummary(stringValue);
            } else {
                preference.setSummary(stringValue);
            }
            return true;
        }
    };

//    private void initAutoExport() {
//        autoExportPre = (ListPreference) findPreference("scales_auto_export_records_cycle");
//        autoExportPre.setSummary(autoExportPre.getEntry());
//        autoExportPre.setOnPreferenceChangeListener((preference, newValue) -> {
//            ListPreference pre = (ListPreference) preference;
//            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
//            return true;
//        });
//    }


//    private void intervalRecord() {
//        intervalRecord = (ListPreference) findPreference("interval_recording");
//        intervalRecord.setSummary(intervalRecord.getEntry());
//        intervalRecord.setOnPreferenceChangeListener((preference, newValue) -> {
//            ListPreference pre = (ListPreference) preference;
//            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
//            return true;
//        });
//    }

//    private void initBeepSound() {
//        beepSound = (ListPreference) findPreference("beep_sound");
//        beepSound.setSummary(beepSound.getEntry());
//        beepSound.setOnPreferenceChangeListener((preference, newValue) -> {
//            ListPreference pre = (ListPreference) preference;
//            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
//            return true;
//        });
//    }

//    private void initOnOffRecordDate() {
//        onlineOfflineRecordData = (ListPreference) findPreference("online_offline_record_data");
//        onlineOfflineRecordData.setSummary(onlineOfflineRecordData.getEntry());
//        onlineOfflineRecordData.setOnPreferenceChangeListener((preference, newValue) -> {
//            ListPreference pre = (ListPreference) preference;
//            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
//            return true;
//        });
//    }


    private void initShowPrice() {
        scalesShowPrice = (ListPreference) findPreference("scales_show_price");
        scalesShowPrice.setSummary(scalesShowPrice.getEntry());
        scalesShowPrice.setOnPreferenceChangeListener((preference, newValue) -> {
            ListPreference pre = (ListPreference) preference;
            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
            return true;
        });
    }



    private void initForceRecordStable() {
        scalesForceRecordStable = (ListPreference) findPreference("scales_force_record_stable");
        scalesForceRecordStable.setSummary(scalesForceRecordStable.getEntry());
        scalesForceRecordStable.setOnPreferenceChangeListener((preference, newValue) -> {
            ListPreference pre = (ListPreference) preference;
            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
            return true;
        });
    }
    private void initForceRecord() {
        scalesForceRecord = (ListPreference) findPreference("scales_force_record");
        scalesForceRecord.setSummary(scalesForceRecord.getEntry());
        scalesForceRecord.setOnPreferenceChangeListener((preference, newValue) -> {
            ListPreference pre = (ListPreference) preference;
            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
            return true;
        });
    }
    private void initScalesloadAuto() {
        scalesloadAuto = (ListPreference) findPreference("scales_load_auto");
        scalesloadAuto.setSummary(scalesloadAuto.getEntry());
        scalesloadAuto.setOnPreferenceChangeListener((preference, newValue) -> {
            ListPreference pre = (ListPreference) preference;
            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
            return true;
        });
    }

//    private void initScalesSwitchServer() {
//        scalesSwitchServer = (ListPreference) findPreference("scales_switch_server");
//        scalesSwitchServer.setSummary(scalesSwitchServer.getEntry());
//        scalesSwitchServer.setOnPreferenceChangeListener((preference, newValue) -> {
//            ListPreference pre = (ListPreference) preference;
//            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
//            return true;
//        });
//    }
//    private void initReadCardProduct() {
//        readCardProduct = (ListPreference) findPreference("read_card_product");
//        readCardProduct.setSummary(readCardProduct.getEntry());
//        readCardProduct.setOnPreferenceChangeListener((preference, newValue) -> {
//            ListPreference pre = (ListPreference) preference;
//            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
//            return true;
//        });
//    }
    private void initShowProductPlu() {
//        showProductPlu = (ListPreference) findPreference("show_product_plu");
//        showProductPlu.setSummary(showProductPlu.getEntry());
//        showProductPlu.setOnPreferenceChangeListener((preference, newValue) -> {
//            ListPreference pre = (ListPreference) preference;
//            pre.setSummary(pre.getEntries()[pre.findIndexOfValue((String) newValue)]);
//            return true;
//        });
    }
    private void initMistakeTouchPreference() {

//        LogUtils.d("jinru==============");

//        scalesForceRecordStable = (SwitchPreference) findPreference("scales_force_record_stable");

//        scalesSwitchServer = (SwitchPreference) findPreference("scales_switch_server");

//        mMistakeTouchPreference = (SwitchPreference) findPreference(MISTAKE_TOUCH_MODE_KEY);


//        scalesLoadAutoSave = (SwitchPreference) findPreference("scales_load_auto");

//        readCardProduct = (SwitchPreference) findPreference("read_card_product");

//        openPrint = (SwitchPreference) findPreference("open_print");

//        openCom = (SwitchPreference) findPreference("open_com");


//        mMistakeTouchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
//            @Override
//            public boolean onPreferenceChange(Preference preference, Object newValue) {
//                final String key = preference.getKey();
//                LogUtils.d("key======" + key + "newValue==" + newValue);
//                if (MISTAKE_TOUCH_MODE_KEY.equals(key)) {
//                    if (mMistakeTouchPreference.isChecked() != (Boolean) newValue) {
//                        boolean value = (Boolean) (newValue);
//                        mMistakeTouchPreference.setChecked(value);
////                        scalesForceRecordStable.setChecked(!value);
//                        //最自己的设置
//                    }
//                }
//
//                return true;
//            }
//        });


//        scalesForceRecordStable.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
//            @Override
//            public boolean onPreferenceChange(Preference preference, Object newValue) {
//                final String key = preference.getKey();
//                LogUtils.d("key======" + key + "newValue==" + newValue);
//                if ("scales_force_record_stable".equals(key)) {
//                    if (scalesForceRecordStable.isChecked() != (Boolean) newValue) {
//                        boolean value = (Boolean) (newValue);
//                        scalesForceRecordStable.setChecked(value);
////                        mMistakeTouchPreference.setChecked(!value);
//                        //最自己的设置
//                    }
//                }
//
//
//                return true;
//            }
//        });

//        scalesLoadAutoSave.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
//            @Override
//            public boolean onPreferenceChange(Preference preference, Object newValue) {
//                final String key = preference.getKey();
//                LogUtils.d("key======" + key + "newValue==" + newValue);
//                if ("scales_load_auto".equals(key)) {
//                    if (scalesLoadAutoSave.isChecked() != (Boolean) newValue) {
//                        boolean value = (Boolean) (newValue);
//                        scalesLoadAutoSave.setChecked(value);
////                        mMistakeTouchPreference.setChecked(!value);
//                        //最自己的设置
//                    }
//                }
//
//
//                return true;
//            }
//        });


//        scalesSwitchServer.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
//            @Override
//            public boolean onPreferenceChange(Preference preference, Object newValue) {
//                final String key = preference.getKey();
//                LogUtils.d("key======" + key + "newValue==" + newValue);
//                if ("scales_switch_server".equals(key)) {
//                    if (scalesSwitchServer.isChecked() != (Boolean) newValue) {
//                        boolean value = (Boolean) (newValue);
//                        scalesSwitchServer.setChecked(value);
//                    }
//                }
//
//
//                return true;
//            }
//        });

//        readCardProduct.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
//            @Override
//            public boolean onPreferenceChange(Preference preference, Object newValue) {
//                final String key = preference.getKey();
//                LogUtils.d("key======" + key + "newValue==" + newValue);
//                if ("read_card_product".equals(key)) {
//                    if (readCardProduct.isChecked() != (Boolean) newValue) {
//                        boolean value = (Boolean) (newValue);
//                        readCardProduct.setChecked(value);
////                        mMistakeTouchPreference.setChecked(!value);
//                        //最自己的设置
//                    }
//                }
//
//                return true;
//            }
//        });

//        openPrint.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
//            @Override
//            public boolean onPreferenceChange(Preference preference, Object newValue) {
//                final String key = preference.getKey();
////                LogUtils.d("key======" + key + "newValue==" + newValue);
//                if ("open_print".equals(key)) {
//                    if (openPrint.isChecked() != (Boolean) newValue) {
//                        boolean value = (Boolean) (newValue);
//                        openPrint.setChecked(value);
////                        mMistakeTouchPreference.setChecked(!value);
//                        //最自己的设置
//                    }
//                }
//
//
//                return true;
//            }
//        });
//        openCom.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
//            @Override
//            public boolean onPreferenceChange(Preference preference, Object newValue) {
//                final String key = preference.getKey();
////                LogUtils.d("key======" + key + "newValue==" + newValue);
//                if ("open_com".equals(key)) {
//                    if (openCom.isChecked() != (Boolean) newValue) {
//                        boolean value = (Boolean) (newValue);
//                        openCom.setChecked(value);
////                        mMistakeTouchPreference.setChecked(!value);
//                        //最自己的设置
//                    }
//                }
//
//
//                return true;
//            }
//        });


    }



}
